package diploma.kisel.modelas.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFileChooser;
import javax.swing.SpringLayout;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.UIManager;

import net.sf.jasperreports.engine.JasperReport;

import diploma.kisel.modelas.gui.common.CurrentVar;
import diploma.kisel.modelas.gui.common.Localization;
import diploma.kisel.modelas.reports.JDBCReport;
import diploma.kisel.modelas.reports.ReportsType;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;

/**
 * It is the main window that contains system menu.
 * 
 * @author Nikolay Kisel
 *
 */
public class MainWindow extends JFrame {

	private JPanel contentPane;

	private static MainWindow frame;

	/**
	 * Create the frame.
	 */
	public MainWindow() {
		setTitle("ModelAS");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu menuExamples = new JMenu("Примеры");
		menuBar.add(menuExamples);

		JMenu menuProjects = new JMenu("Мои проекты");
		menuBar.add(menuProjects);

		JMenuItem menuItemNewProject = new JMenuItem("Новый проект");
		menuItemNewProject.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openNewProjectWindow();
			}
		});
		menuProjects.add(menuItemNewProject);
		
		JMenuItem menuItemChooseProject = new JMenuItem("Выбрать текущий");
		menuItemChooseProject.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				openCurrentProjectWindow();
			}
		});
		menuProjects.add(menuItemChooseProject);
		
		JMenu menuConcepts = new JMenu("Мои концепции");
		menuBar.add(menuConcepts);
		
		JMenuItem menuItemNewConcept = new JMenuItem("Новая концепция");
		menuItemNewConcept.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				openNewConceptWindow();
			}
		});
		menuConcepts.add(menuItemNewConcept);
		
		JMenuItem menuItemChooseConcept = new JMenuItem("Выбрать текущую");
		menuItemChooseConcept.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openCurrentConceptWindow();
			}
		});
		menuConcepts.add(menuItemChooseConcept);
		
		JMenu mnEditConcept = new JMenu("Редактировать концепцию");
		menuConcepts.add(mnEditConcept);
		
		JMenuItem menuItemAddServers = new JMenuItem("Добавить серверы");
		menuItemAddServers.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				openServerWindow();
			}
		});
		mnEditConcept.add(menuItemAddServers);
		
		JMenuItem menuItem = new JMenuItem("Добавить РМ");
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openWorkplaceWindow();
			}
		});
		mnEditConcept.add(menuItem);
		
		JMenu mnShowConcept = new JMenu("Просмотреть концепцию");
		menuConcepts.add(mnShowConcept);
		
		JMenuItem menuItemShowServers = new JMenuItem("Просмотреть серверы");
		menuItemShowServers.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				openServerCatalogWindos();
			}
		});
		mnShowConcept.add(menuItemShowServers);
		
		JMenuItem menuItemShowWP = new JMenuItem("Просмотреть РМ");
		mnShowConcept.add(menuItemShowWP);
		
		JMenu menu = new JMenu("Отчеты");
		menuBar.add(menu);
		
		JMenu menuCatalogs = new JMenu("По справочникам");
		menu.add(menuCatalogs);
		
		JMenuItem menuItemPC = new JMenuItem("По ПЭВМ");
		menuItemPC.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				saveComputerReport();
			}
		});
		menuCatalogs.add(menuItemPC);
		
		JMenuItem menuItemDevice = new JMenuItem("По устройствам");
		menuItemDevice.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveDeviceReport();
			}
		});
		menuCatalogs.add(menuItemDevice);
		
		JMenuItem menuItemProgram = new JMenuItem("По программам");
		menuItemProgram.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveProgramReport();
			}
		});
		menuCatalogs.add(menuItemProgram);
		
		JMenu menuСoncept = new JMenu("По концепции");
		menu.add(menuСoncept);
		
		JMenuItem menuItemServers = new JMenuItem("По серверам");
		menuItemServers.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveServerReport();
			}
		});
		menuСoncept.add(menuItemServers);
		
		JMenuItem menuItemWp = new JMenuItem("По РМ");
		menuItemWp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				saveWorkplaceReport();
			}
		});
		menuСoncept.add(menuItemWp);
		
		JMenu menuNote = new JMenu("Справка");
		menuBar.add(menuNote);
		
		JMenu menuAboutProgram = new JMenu("О программе");
		menuAboutProgram.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				openAboutProgramWindow();				
			}
		});
		
		menuBar.add(menuAboutProgram);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
		
		JLabel lblNewLabel = new JLabel("Model AS");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel, 22, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, lblNewLabel, -89, SpringLayout.EAST, contentPane);
		lblNewLabel.setFont(new Font("Monotype Corsiva", Font.BOLD | Font.ITALIC, 62));
		contentPane.add(lblNewLabel);
		
		JLabel label = new JLabel("Программа для создания концепций АС");
		label.setFont(new Font("Lucida Sans Unicode", Font.PLAIN, 11));
		sl_contentPane.putConstraint(SpringLayout.NORTH, label, 3, SpringLayout.SOUTH, lblNewLabel);
		sl_contentPane.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST, lblNewLabel);
		contentPane.add(label);
	}

	public static MainWindow getFrame() {
		return frame;
	}

	public static void setFrame(MainWindow frame) {
		MainWindow.frame = frame;
	}
	
	public void openCurrentProjectWindow() {
		CurrentProjectWindow currentProjectWindow = new CurrentProjectWindow();
		CurrentProjectWindow.setFrame(currentProjectWindow);
		currentProjectWindow.init();
		currentProjectWindow.setVisible(true);
	}
	
	public void openCurrentConceptWindow() {
		CurrentConceptWindow currentConceptWindow = new CurrentConceptWindow();
		currentConceptWindow.setFrame(currentConceptWindow);
		currentConceptWindow.setVisible(true);
	}
	
	public void openNewConceptWindow() {
		ConceptWindow conceptWindow = new ConceptWindow();
		ConceptWindow.setFrame(conceptWindow);
		conceptWindow.setVisible(true);
	}
	
	public void openNewProjectWindow() {
		ProjectWindow projectWindow = new ProjectWindow();
		ProjectWindow.setFrame(projectWindow);
		projectWindow.setVisible(true);
	}
	
	public void openServerWindow() {
		ServerWindow serverWindow = new ServerWindow();
		ServerWindow.setFrame(serverWindow);
		serverWindow.setVisible(true);
	}
	
	public void openWorkplaceWindow() {
		WorkplaceWindow workplaceWindow = new WorkplaceWindow();
		WorkplaceWindow.setFrame(workplaceWindow);
		workplaceWindow.setVisible(true);
	}
	
	public void openServerCatalogWindos() {
		ServerCatalogWindow serverCatalogWindow = new ServerCatalogWindow();
		ServerCatalogWindow.setFrame(serverCatalogWindow);
		serverCatalogWindow.setVisible(true);
	}
	
	public void openAboutProgramWindow() {
		AboutProgramWindow aboutProgramWindow = new AboutProgramWindow();
		AboutProgramWindow.setFrame(aboutProgramWindow);
		aboutProgramWindow.setVisible(true);
	}
	
	public void saveComputerReport() {
		Localization.fileChooserLocalization();				
		JFileChooser fileChooser = new JFileChooser();				
		int result = fileChooser.showSaveDialog(frame);
		fileChooser.updateUI();
		if (result == JFileChooser.APPROVE_OPTION) {					
			JDBCReport jdbcReport = new JDBCReport();
			File file = fileChooser.getSelectedFile();
			jdbcReport.writeRepot(file, ReportsType.AllPC, 0, new ArrayList());				
		}
	}
	
	public void saveDeviceReport() {
		Localization.fileChooserLocalization();				
		JFileChooser fileChooser = new JFileChooser();				
		int result = fileChooser.showSaveDialog(frame);
		fileChooser.updateUI();
		if (result == JFileChooser.APPROVE_OPTION) {					
			JDBCReport jdbcReport = new JDBCReport();
			File file = fileChooser.getSelectedFile();
			jdbcReport.writeRepot(file, ReportsType.AllDevices, 0, new ArrayList());				
		}
	}
	
	public void saveProgramReport() {
		Localization.fileChooserLocalization();				
		JFileChooser fileChooser = new JFileChooser();				
		int result = fileChooser.showSaveDialog(frame);
		fileChooser.updateUI();
		if (result == JFileChooser.APPROVE_OPTION) {					
			JDBCReport jdbcReport = new JDBCReport();
			File file = fileChooser.getSelectedFile();
			jdbcReport.writeRepot(file, ReportsType.AllProgram, 0, new ArrayList());				
		}
	}
	
	public void saveServerReport() {
		Localization.fileChooserLocalization();				
		JFileChooser fileChooser = new JFileChooser();				
		int result = fileChooser.showSaveDialog(frame);
		fileChooser.updateUI();
		if (result == JFileChooser.APPROVE_OPTION) {					
			JDBCReport jdbcReport = new JDBCReport();
			File file = fileChooser.getSelectedFile();
			List<Object> params= new ArrayList<>();
			params.add(CurrentVar.getCurrentConcept().getId());
			jdbcReport.writeRepot(file, ReportsType.Servers, 1, params);				
		}
	}
	
	public void saveWorkplaceReport() {
		Localization.fileChooserLocalization();				
		JFileChooser fileChooser = new JFileChooser();				
		int result = fileChooser.showSaveDialog(frame);
		fileChooser.updateUI();
		if (result == JFileChooser.APPROVE_OPTION) {					
			JDBCReport jdbcReport = new JDBCReport();
			File file = fileChooser.getSelectedFile();
			List<Object> params= new ArrayList<>();
			params.add(CurrentVar.getCurrentConcept().getId());
			jdbcReport.writeRepot(file, ReportsType.Workplace, 2, params);				
		}
	}
}
